package com.paiai.lslease.service.apphouse;

import com.paiai.lslease.auto.dao.AppLockTenantDAO;
import com.paiai.lslease.auto.model.AppLock;
import com.paiai.lslease.auto.model.AppLockTenant;
import com.paiai.lslease.auto.model.AppLockTenantExample;
import com.paiai.lslease.auto.model.AppSysUser;
import com.paiai.lslease.dao.apphouse.AppLockTenantInfoDAO;
import com.paiai.lslease.model.AppLockTenantInfo;
import com.paiai.lslease.service.appuser.AppSysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service("appLockTenantService")
public class AppLockTenantServiceImpl implements AppLockTenantService {

    private static final Logger logger = LoggerFactory.getLogger(AppLockTenantServiceImpl.class);

    private AppLockTenantDAO appLockTenantDAO;
    private AppSysUserService appSysUserService;
    private AppLockTenantInfoDAO appLockTenantInfoDAO;
    private AppLockService appLockService;

    public void setAppLockTenantDAO(AppLockTenantDAO appLockTenantDAO) {
        this.appLockTenantDAO = appLockTenantDAO;
    }

    public void setAppSysUserService(AppSysUserService appSysUserService) {
        this.appSysUserService = appSysUserService;
    }

    public void setAppLockTenantInfoDAO(AppLockTenantInfoDAO appLockTenantInfoDAO) {
        this.appLockTenantInfoDAO = appLockTenantInfoDAO;
    }

    public void setAppLockService(AppLockService appLockService) {
        this.appLockService = appLockService;
    }

    public AppLockTenant insertLockTenant(AppLockTenant appLockTenant) throws Exception {
        //用户ID表示的用户是否注册
        if (!appSysUserService.checkUserRegisterState(appLockTenant.getTenantId())) {
            logger.info("用户ID表示的用户未注册");
            return null;
        }
        //设备ID表示的设备是否存在
        if (appLockService.selectLockById(appLockTenant.getLockId()) == null) {
            logger.info("设备ID表示的设备不存在");
            return null;
        }
        //将用户角色修改为租客
        if (!appSysUserService.addTenantRoleToUserById(appLockTenant.getTenantId())) {
            logger.info("将用户角色修改为租客失败");
            return null;
        }
        //保存租客租房信息
        appLockTenant.setItem1("1");
        appLockTenant.setInsertTime(new Date());
        appLockTenant.setUpdateTime(new Date());
        appLockTenantDAO.insert(appLockTenant);
        return selectLockTenant(appLockTenantInfoDAO.selectNeweastAppLockTenantId());
    }

    public boolean deleteLockTenant(int lockTenantId) throws Exception {
        //先根据租客设备ID获取到租客设备详情
        AppLockTenant appLockTenant = appLockTenantDAO.selectByPrimaryKey(lockTenantId);
        if (appLockTenant == null) {
            logger.info("根据租客设备ID获取租客设备详情失败");
            return false;
        }
        //获取该租客的所有租客设备设备列表
        AppLockTenantExample appLockTenantExample = new AppLockTenantExample();
        appLockTenantExample.createCriteria().andTenantIdEqualTo(appLockTenant.getTenantId()).andItem1EqualTo("1");
        List<AppLockTenant> appLockTenantList = appLockTenantDAO.selectByExample(appLockTenantExample);
        if (appLockTenantList == null || appLockTenantList.size() == 0) {
            logger.info("获取该租客的所有租客设备设备列表失败");
            return false;
        }
        if (appLockTenantList.size() == 1) {//去掉租客的最后一个设备时，去掉用户的租客角色
            if (!appSysUserService.deleteTenantRoleToUser(appLockTenant.getTenantId())) {
                logger.info("去掉用户的租客角色失败");
                return false;
            }
        }
        appLockTenant.setItem1("-1");
        appLockTenant.setUpdateTime(new Date());
        return appLockTenantDAO.updateByPrimaryKey(appLockTenant) > 0;
    }

    @Override
    public boolean deleteLockTenant(int tenantId, int lockId) throws Exception {
        //将记录标记为删除
        AppLockTenantExample appLockTenantExample = new AppLockTenantExample();
        appLockTenantExample.createCriteria().andTenantIdEqualTo(tenantId).andLockIdEqualTo(lockId).andItem1EqualTo("1");
        AppLockTenant appLockTenant = new AppLockTenant();
        appLockTenant.setItem1("-1");
        appLockTenantDAO.updateByExampleSelective(appLockTenant, appLockTenantExample);
        //查询该用户是否还有租住记录
        appLockTenantExample.clear();
        appLockTenantExample.createCriteria().andTenantIdEqualTo(tenantId).andItem1EqualTo("1");
        List<AppLockTenant> appLockTenantList = appLockTenantDAO.selectByExample(appLockTenantExample);
        if (appLockTenantList == null || appLockTenantList.size() == 0) {//没有租住记录了，去掉用户的租客属性
            appSysUserService.deleteTenantRoleToUser(tenantId);
        }
        return true;
    }

    public boolean deleteAllLockTenantByLockId(int lockId) throws Exception {
        AppLockTenantExample appLockTenantExample = new AppLockTenantExample();
        appLockTenantExample.createCriteria().andLockIdEqualTo(lockId).andItem1EqualTo("1");
        List<AppLockTenant> appLockTenantList = appLockTenantDAO.selectByExample(appLockTenantExample);
        if (appLockTenantList != null) {
            for (AppLockTenant appLockTenant : appLockTenantList) {
                if (!deleteLockTenant(appLockTenant.getId())) {
                    logger.info("清空租客时，删除租客关联ID=" + appLockTenant.getId() + "失败");
                    return false;
                }
            }
        }
        return true;
    }

    public AppLockTenant modifyLockTenant(AppLockTenant appLockTenant) throws Exception {
        appLockTenant.setUpdateTime(new Date());
        if (appLockTenantDAO.updateByPrimaryKeySelective(appLockTenant) <= 0) {
            logger.info("更新租客信息失败");
            return null;
        }
        return selectLockTenant(appLockTenant.getId());
    }

    public List<AppLockTenantInfo> selectAllLockTenant(int lockId) throws Exception {
//        AppLockTenantExample appLockTenantExample = new AppLockTenantExample();
//        appLockTenantExample.createCriteria().andLockIdEqualTo(lockId).andItem1EqualTo("1");
        return appLockTenantInfoDAO.selectAllLockTenantInfoByLockId(lockId + "");
    }

    public List<AppLock> selectAllTenantLock(int tenantId) throws Exception {
        return appLockTenantInfoDAO.selectAllLockByTenantId(tenantId);
    }

    public AppLockTenant selectLockTenant(int lockTenantId) throws Exception {
        AppLockTenant appLockTenant = appLockTenantDAO.selectByPrimaryKey(lockTenantId);
        return appLockTenant != null && "1".equalsIgnoreCase(appLockTenant.getItem1()) ? appLockTenant : null;
    }

    @Override
    public boolean userHasLock(String mobile, int lockId) throws Exception {
        AppSysUser appSysUser = appSysUserService.getAppSysUserInfoByMobile(mobile);
        if (appSysUser != null) {
            AppLockTenantExample appLockTenantExample = new AppLockTenantExample();
            appLockTenantExample.createCriteria().andTenantIdEqualTo(appSysUser.getId()).andLockIdEqualTo(lockId).andItem1EqualTo("1");
            List<AppLockTenant> appLockTenantList = appLockTenantDAO.selectByExample(appLockTenantExample);
            return appLockTenantList != null && appLockTenantList.size() > 0;
        }
        return false;
    }

    @Override
    public boolean tenantExpired(int tenantId, int lockId) throws Exception {
        AppLockTenantExample appLockTenantExample = new AppLockTenantExample();
        appLockTenantExample.createCriteria().andTenantIdEqualTo(tenantId).andLockIdEqualTo(lockId).andItem1EqualTo("1");
        List<AppLockTenant> appLockTenantList = appLockTenantDAO.selectByExample(appLockTenantExample);
        if (appLockTenantList != null && appLockTenantList.size() > 0) {
            AppLockTenant appLockTenant = appLockTenantList.get(0);//一个租客，一个房间，同一时间只能有一条租住记录
            Date deadDate = appLockTenant.getDeadDate();
            if (deadDate != null && deadDate.getTime() < new Date().getTime()) {
                return true;
            }
        }
        return false;
    }
}
